package co.edu.usbcali.presentation.backingBeans;


import java.util.List;

import co.edu.usbcali.parcial3.Usuario;
import co.edu.usbcali.presentation.businessDelegate.BusinessDelegatorView;
import co.edu.usbcali.utilities.FacesUtils;

import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;


public class InicioView {

	private HtmlInputText txtLogin;
	private HtmlInputText txtPassword;
	private HtmlCommandButton btnIngresar;

	Usuario usu;

	public String action_iniciarSesion() throws Exception {
		return iniciarSesion();
	}

	private String iniciarSesion() {
		String mensaje = "";

		try {
			String txtUsu = FacesUtils.checkString(txtLogin);
			String txtCon = FacesUtils.checkString(txtPassword);

			List<Usuario> u = BusinessDelegatorView.getUsuario2(txtUsu, txtCon);

			if (u.size() != 0) {
				usu = u.get(0);
				mensaje = "Iniciar";
			}else {
				throw new Exception("Usuario o Password Incorrectos");
			}
			FacesUtils.getSession().setAttribute("usuario", usu);
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}
		return mensaje;
	}

	public String action_cerrarSesion() throws Exception {

		FacesUtils.getSession().setAttribute("usuario", null);
		return "goInicio";
	}

	public HtmlInputText getTxtLogin() {
		return txtLogin;
	}

	public void setTxtLogin(HtmlInputText txtLogin) {
		this.txtLogin = txtLogin;
	}

	public HtmlInputText getTxtPassword() {
		return txtPassword;
	}

	public void setTxtPassword(HtmlInputText txtPassword) {
		this.txtPassword = txtPassword;
	}

	public HtmlCommandButton getBtnIngresar() {
		return btnIngresar;
	}

	public void setBtnIngresar(HtmlCommandButton btnIngresar) {
		this.btnIngresar = btnIngresar;
	}

	

}
